// index.js
import getColorPair from "random-color-pair";
const app = getApp()

Page({
  data: {
    foods:[],
    selectedFood:'',
    count:0,

  },
  
  onLoad() {
    this.initFood()
  },
  initFood(){
    let arr = ['披萨','炸鸡','混沌','麻辣烫','啤酒']
    let newArr = arr.map(item=>{
      const [foreground, background] = getColorPair();
      let f = {}
      f.name=item
      f.active=false
      f.foreground=foreground
      f.background=background
      f.top=(Math.random() * 20).toFixed(0)
      f.delay=(Math.random() * 4).toFixed(1)
      return f
    })
    this.setData({
      foods:newArr
    })
  },
  start(){
    this.setData({
      selectedFood:''
    })
    if(this.data.count===0){
      this.addShaddow(0)
    }
    if(this.data.count===25){
      this.setData({
        count:0
      })
      this.addShaddow(0)
    }
  },
  addShaddow(i){
    if(this.data.count===25){
      let activeItem = this.data.foods.filter(item=>item.active)
      console.log(activeItem)
      this.setData({
        selectedFood:activeItem[0].name
      })
      return
    }
    let arr = this.data.foods.map(item=>{
      item.active=false
      return item
    })
    arr[i].active=true
    this.setData({
      foods:arr,
      count:this.data.count+1
    })
    let length = this.data.foods.length
      let random =(Math.random() * length).toFixed(0)
      if(random == length) random=length-1
    if(random==1){
      this.addShaddow(random)
    }else{
      setTimeout(()=>{
        this.addShaddow(random)
      },200)
    }
  }
})
